package com.demo.rbactest.handle;

import com.demo.rbactest.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 统一异常处理
 */

@RestControllerAdvice
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    //    通用异常处理
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R errorHandler(Exception e) {
        final StackTraceElement[] stackTrace = e.getStackTrace();
        log.error("捕获到异常 => {}, 异常的位置 => {}", e.getMessage(), stackTrace[0]);
//        控制台输出错误位置
        e.printStackTrace();
        return R.error("失败原因:" + e.getMessage());
    }

}
